/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.groovy

/**
 *
 * @author lukasz
 */
class Regex
{
    public static List<String> getTemplateMacros(String template)
    {
        def regex = /\$\{.*?\}/;

        def matcher = (template =~ regex);

        def result = new ArrayList<String>();

        for (def i=0; i<matcher.size(); i++)
            result.add(matcher[i]);

        return result;
    }


    public static String getPackageNameFromSource(String source)
    {
        def regex = /package\s+([\w\.]+);?\s*.*/
        def packName = null

        source.eachLine { line ->

            if (line ==~ regex)
                packName = (line =~ regex)[0][1]
                
        }

        if (packName != null)
            return packName
        else
            return ""
    }

    public static boolean validateProjectName(String name)
    {
        return name ==~ /[a-zA-Z][\w]*/;
    }

    public static boolean validateClassName(String name)
    {
        return name ==~ /[a-zA-Z][\w]*/;
    }

    public static boolean validatePackageName(String name)
    {
        return name ==~ /([a-zA-Z][\w]*)(\.[a-zA-Z][\w]*)*/;
    }    

    public static String extractClassName(String fullName)
    {
        String[] ids = fullName.split("\\.")
        
        if (ids.length > 0)
            return ids[ids.length - 1]
        else
            return "";
    }

    public static String extractPackageName(String fullName)
    {
        String[] ids = fullName.split("\\.")

        if (ids.length < 2)
            return "";
        else
        {
            String x = "";
            for (int i=0; i<ids.length-1; i++)
                x += (ids[i] + ".");            

            return x.substring(0, x.length()-1);
        }
    }

    static String getFileExtension(String filename)
    {
        if (filename ==~ /.*\.([a-zA-Z0-9]*)$/)
            return (filename =~ /.*\.([a-zA-Z0-9]*)$/)[0][1];
        else
            return "";
    }

	
}

